package cn.tedu.eb_film.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;

import java.util.List;
import java.util.Map;

/**
 * @author diaojie
 * 监听器,监听前端提交的表格，并将内容交给list集合
 */
@Slf4j
public class GeneralListener<T> extends AnalysisEventListener<T> {

    private List<T> list = Lists.newArrayList();

    @Override
    public void invoke(T data, AnalysisContext analysisContext) {
        if(data==null||data.equals("")
        ){
            log.info("解析到空白行，跳过");
            return;
        }
        log.info("解析到一条头数据:{}", JSON.toJSONString(data));
        //将表中数据添加到list中
        list.add(data);
    }

    // 读取表头内容
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        log.info("表头：{}", headMap);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("read data complete!");
    }

    public List<T> getList() {
        return list;
    }
}
